package progetto;

import peersim.cdsim.CDProtocol;
import peersim.config.Configuration;
import peersim.core.CommonState;
import peersim.core.Node;

public class Disinfection implements CDProtocol{

	// ------------------------------------------------------------------------
    // Parameters
    // ------------------------------------------------------------------------
    
	/**
	 * 
	 * @config
	 */
	private static final String PAR_PROB_TEMP = "prob_temp"; 
	
	
    /**
     * 
     * @config
     */
    private static final String PAR_PROB_IMMUNE = "prob_immune";
    
    private static double PROB_TEMP_DEF = 20;
    
    /**
     * @config
     */
    private static final String PAR_STATUS_PROT = "status_prot";
    
    private static double PROB_IMMUNE_DEF = 1;
    
    
    // ------------------------------------------------------------------------
    // Fields
    // ------------------------------------------------------------------------

    /**
     * Obtained from config property
     * {@link PAR_PROB_TEMP}.
     */
	private final double percentageTemp;
	
	/**
     * Obtained from config property
     * {@link PAR_PROB_IMMUNE}.
     */
	private final double percentageImmune;
	
	/**
     * Obtained from config property
     * {@link PAR_STATUS_PROT}.
     */
	private final int statusPid;
	
	private final double tempProb;
	
	private final double immuneProb;
	
	
	
	public Disinfection(String prefix) {
		double percentageTemp = Configuration.getDouble(prefix + "."+ PAR_PROB_TEMP, PROB_TEMP_DEF);
		if(percentageTemp > 100 || percentageTemp < 0) {
			this.percentageTemp = PROB_TEMP_DEF;
			System.err.println("The value of the temporary immunity isn't corret: used the default value of "+ PROB_TEMP_DEF);
			
		} else {
			this.percentageTemp = percentageTemp;
		}
		
		double percentageImmune = Configuration.getDouble(prefix + "."+ PAR_PROB_IMMUNE, PROB_IMMUNE_DEF);
		if(percentageImmune > 100 || percentageImmune < 0) {
			this.percentageImmune = PROB_IMMUNE_DEF;
			System.err.println("The value of the temporary immunity isn't corret: used the default value of "+ PROB_IMMUNE_DEF);
			
		} else {
			this.percentageImmune = percentageImmune;
		}
		statusPid = Configuration.getPid(prefix + "."+ PAR_STATUS_PROT);
		
		tempProb = this.getNormalizedValue(percentageTemp);
		immuneProb = this.getNormalizedValue(percentageImmune);
		
		
	}

	
	public double getNormalizedValue(double percentage) {
		
		return percentage / (double)100;
	}

	@Override
	public void nextCycle(Node node, int protocolID) {
		// TODO Auto-generated method stub
		//int value = (int)(1 + (1 + 10 - 1)*CommonState.r.nextDouble());
		
		//System.out.println("value "+ value + "run "+ CommonState.getIntTime());
		NodeStatus status = (NodeStatus)node.getProtocol(statusPid);
		if(status.isInfected()) {
			//number random between 1 and 10 that representing the number of runs from which the node to be at least infected
			int randRun = CommonState.r.nextInt(10) + 1;
			double tempValue = CommonState.r.nextDouble();
			if(tempValue < tempProb && (CommonState.getIntTime() - status.getRunInfec()) >= randRun) {
				status.setInfected(false);
				//System.out.println("mi disinfetto momentaneamente");
				
			}
			double permValue = CommonState.r.nextDouble();
			/*if(permValue < immuneProb) {
				System.out.println(" immValue "+ immValue+ " immuneProb "+ immuneProb);
				status.setInfected(false);
				//immune!!!!
				System.out.println("Immuneeee!!!!");
				status.setImmune(true);
			}*/
		}
	}
	
	
	
	
	
	/**
	 * Clone.
	 */
	public Object clone()
	{
		Disinfection inf = null;
		try { 
			inf = (Disinfection)super.clone(); }
		catch( CloneNotSupportedException e ) {} // never happens
		return inf;
	}

}
